package cn.yeyiu.netonlinevideo.parser.video;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.netonlinevideo.util.AESUtil;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.VideoInfo;

public class HeiheiParser extends Parser {

    Map<String, String> typeMap = new HashMap<>();
    private final String AES_PASSWORD = "56+0x4d8=8c56fd5";

    public HeiheiParser(SourceData source) {
        super(source);
    }

    @Override
    public List<TypeData> getTypeList() {
        String typeurl = "http://vapi.pipe-welding.cn/api/getVideoCategory";
        String html = NetUtil.postByUrl(typeurl, null, true);
        String aesData = JSONObject.parseObject(html).getString("result");
        String json = AESUtil.decrypt(aesData, AES_PASSWORD);
        JSONArray array = JSONArray.parseArray(json);

        List<TypeData> types = new ArrayList<>();
        for (int i=0;i<array.size();++i) {
            JSONObject object = array.getJSONObject(i);
            String categoryId = object.getString("id");
            String type = object.getString("title");
            types.add(new TypeData(type));
            typeMap.put(type, categoryId);
        }
        return types;
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        String url = "http://vapi.pipe-welding.cn/api/h5/getListByCategory";
        String body = "{'sort':'-1','categoryId':'%s','page':'%d','limit':%d}";

        String categoryId = typeMap.get(type);
        body = String.format(Locale.CHINA, body, categoryId, pageIndex+1, source.pageSize);
        body = AESUtil.encrypt(body, AES_PASSWORD);
        JSONObject bodyData = new JSONObject();
        bodyData.put("data", body);

        String json = NetUtil.postByUrl(url, bodyData, true);
        return JSONObject.parseObject(json);
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        JSONObject jo = (JSONObject) data;
        String result = jo.getString("result");
        String json = AESUtil.decrypt(result, AES_PASSWORD);
        JSONArray jsonArray = JSONObject.parseObject(json).getJSONObject("data").getJSONArray("records");

        List<MediaData> videoInfos = new ArrayList<>();
        MediaData mediaData;
        VideoInfo videoInfo;
        for (int i = 0; i < jsonArray.size(); ++i) {
            videoInfo = new VideoInfo();
            mediaData = new MediaData();

            JSONObject object = jsonArray.getJSONObject(i);
            String id = object.getString("id");
            videoInfo.code = source.code;
            videoInfo.type = type;
            videoInfo.title = object.getString("title");
            videoInfo.picSrc = object.getString("imgUrl");

            mediaData.id = id;
            mediaData.wrap(videoInfo);
            videoInfos.add(mediaData);
        }
        return videoInfos;
    }

    @Override
    public Object buildData(MediaData mediaData) {
        VideoInfo videoInfo = (VideoInfo) mediaData.data;
        //{"userId":"1471740789708386306","id":"1a776fa5e31577854a2466c3a172ee19"}
        String url = "http://vapi.pipe-welding.cn/api/getVideoInfo";
        String body = "{'userId':'%s','id':'%s'}";

        body = String.format(Locale.CHINA, body, "", mediaData.id);
        body = AESUtil.encrypt(body, AES_PASSWORD);
        JSONObject bodyData = new JSONObject();
        bodyData.put("data", body);

        String data = NetUtil.postByUrl(url, bodyData, false);
        data = AESUtil.decrypt(JSONObject.parseObject(data).getString("result"), AES_PASSWORD);
        videoInfo.videoSrc =  JSONObject.parseObject(data).getString("videoUrl");
        return videoInfo;
    }
}
